import { app, BrowserWindow, globalShortcut, ipcMain } from 'electron'
import '../renderer/store'
import os from 'os';
import fs from 'fs';
import Store from 'electron-store';
const store = new Store();
import localshortcut from 'electron-localshortcut';
//运行前检查
const defaultShortcut = {
  cs0: '',
  cs1: '',
  cs2: '',
  cs3: '',
  cs4: '',
  cs5: '',
  cs6: '',
  cs7: '',
  cs8: '',
  cs9: '',
}

const perConf = store.get('config');
if (!perConf) {
  store.set('config', JSON.stringify(defaultShortcut));
}
try {
  JSON.parse(perConf);
} catch (error) {
  store.set('config', JSON.stringify(defaultShortcut));
}
let ctrl = '';
let alt = 'alt';
if (os.platform() == 'darwin') {
  ctrl = 'command';
} else {
  ctrl = 'ctrl';

}




const shortcut = JSON.parse(store.get('config')) || defaultShortcut;

//作为全局变量传递给渲染进程
global.shortcut = shortcut;
global.store = store;
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow() {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 480,
    useContentSize: true,
    width: 490,
    resizable: false
  })

  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })


}

app.on('ready', () => {

  globalShortcut.unregisterAll()

  // mainWindow.webContents.send('shortcutList',shortcut);
  globalShortcut.register(`${ctrl}+shift+1`, function () {
    mainWindow.webContents.send('cs1', shortcut['cs1']);
  });
  globalShortcut.register(`${ctrl}+shift+2`, function () {
    mainWindow.webContents.send('cs2', shortcut['cs2']);
  });
  globalShortcut.register(`${ctrl}+shift+3`, function () {
    mainWindow.webContents.send('cs3', shortcut['cs3']);
  });
  globalShortcut.register(`${ctrl}+shift+4`, function () {
    mainWindow.webContents.send('cs4', shortcut['cs4']);
  });
  globalShortcut.register(`${ctrl}+shift+5`, function () {
    mainWindow.webContents.send('cs5', shortcut['cs5']);
  });
  globalShortcut.register(`${ctrl}+shift+6`, function () {
    mainWindow.webContents.send('cs6', shortcut['cs6']);
  });
  globalShortcut.register(`${ctrl}+shift+7`, function () {
    mainWindow.webContents.send('cs7', shortcut['cs7']);
  });
  globalShortcut.register(`${ctrl}+shift+8`, function () {
    mainWindow.webContents.send('cs8', shortcut['cs8']);
  });
  globalShortcut.register(`${ctrl}+shift+9`, function () {
    mainWindow.webContents.send('cs9', shortcut['cs9']);
  });
  globalShortcut.register(`${ctrl}+shift+0`, function () {
    mainWindow.webContents.send('cs0', shortcut['cs0']);
  });

  globalShortcut.register(`${ctrl}+${alt}+0`, function () {
    mainWindow.webContents.send('alt0', '');
  })

  globalShortcut.register(`${ctrl}+${alt}+1`, function () {
    mainWindow.webContents.send('alt1', '');
  })
  globalShortcut.register(`${ctrl}+${alt}+2`, function () {
    mainWindow.webContents.send('alt2', '');
  })
  globalShortcut.register(`${ctrl}+${alt}+3`, function () {
    mainWindow.webContents.send('alt3', '');
  })
  globalShortcut.register(`${ctrl}+${alt}+4`, function () {
    mainWindow.webContents.send('alt4', '');
  })
  globalShortcut.register(`${ctrl}+${alt}+5`, function () {
    mainWindow.webContents.send('alt5', '');
  })
  globalShortcut.register(`${ctrl}+${alt}+6`, function () {
    mainWindow.webContents.send('alt6', '');
  })
  globalShortcut.register(`${ctrl}+${alt}+7`, function () {
    mainWindow.webContents.send('alt7', '');
  })
  globalShortcut.register(`${ctrl}+${alt}+8`, function () {
    mainWindow.webContents.send('alt8', '');
  })
  globalShortcut.register(`${ctrl}+${alt}+9`, function () {
    mainWindow.webContents.send('alt9', '');
  })
  if(os.platform() == 'darwin') {
    localshortcut.register(`command+v`,function() {
      mainWindow.webContents.paste();
    })
    localshortcut.register(`command+c`,function() {
      mainWindow.webContents.copy();
    })
  }
  

  createWindow();
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

app.on('will-quit', () => {
  // 注销所有快捷键
  globalShortcut.unregisterAll()
})




ipcMain.on('write', (event, arg) => {
  store.set('config', JSON.stringify(arg));
})

ipcMain.on('paste', (event, arg) => {
  console.log('1111 :>> ', 1111);
  mainWindow.webContents.paste();
})

ipcMain.on('copy', (event, arg) => {
  console.log('222 :>> ', 222);
  mainWindow.webContents.copy();
})